package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.concurrent.TimeUnit;

import com.alibaba.fastjson2.JSONObject;
import org.apache.commons.lang3.RandomUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TrainingPlanMapper;
import com.ruoyi.system.domain.TrainingPlan;
import com.ruoyi.system.service.ITrainingPlanService;


/**
 * 培训Service业务层处理
 * 
 * @author fyt
 * @date 2025-03-03
 */
@Service
public class TrainingPlanServiceImpl implements ITrainingPlanService 
{
    private static final Logger log = LoggerFactory.getLogger(TrainingPlanServiceImpl.class);
    @Autowired
    private TrainingPlanMapper trainingPlanMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    private final StringRedisTemplate redisTemplate;

    public TrainingPlanServiceImpl(StringRedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    /**
     * 查询培训
     * 
     * @param id 培训主键
     * @return 培训
     */
    @Override
    public TrainingPlan selectTrainingPlanById(Long id)
    {
        return trainingPlanMapper.selectTrainingPlanById(id);
    }

    /**
     * 查询培训列表
     * 
     * @param trainingPlan 培训
     * @return 培训
     */
    @Override
    public List<TrainingPlan> selectTrainingPlanList(TrainingPlan trainingPlan)
    {
        return trainingPlanMapper.selectTrainingPlanList(trainingPlan);
    }

    /**
     * 新增培训
     * 
     * @param trainingPlan 培训
     * @return 结果
     */
    @Override
    public int insertTrainingPlan(TrainingPlan trainingPlan)
    {
        // 配置消息信息
        String queueName = "plan.manager";
        String massage = "审核成功";
        // 发送消息
//        new Thread(() -> {
//            try {
//                TimeUnit.SECONDS.sleep(5 * 2);
//                rabbitTemplate.convertAndSend(queueName, massage);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        }).start();
        return trainingPlanMapper.insertTrainingPlan(trainingPlan);
    }

    /**
     * 修改培训
     * 
     * @param trainingPlan 培训
     * @return 结果
     */
    @Override
    public int updateTrainingPlan(TrainingPlan trainingPlan)
    {
        return trainingPlanMapper.updateTrainingPlan(trainingPlan);
    }

    /**
     * 批量删除培训
     * 
     * @param ids 需要删除的培训主键
     * @return 结果
     */
    @Override
    public int deleteTrainingPlanByIds(Long[] ids)
    {
        return trainingPlanMapper.deleteTrainingPlanByIds(ids);
    }

    /**
     * 删除培训信息
     * 
     * @param id 培训主键
     * @return 结果
     */
    @Override
    public int deleteTrainingPlanById(Long id)
    {
        return trainingPlanMapper.deleteTrainingPlanById(id);
    }

    @Override
    public TrainingPlan selectTrainingPlanAddRedisById(Long id) {

        String key = "trainingPlan:" + id;
        String strJson = redisTemplate.opsForValue().get(key);
        if (strJson != null) {// 如果缓存存在
            // 数据为空，返回null，解决缓存穿透
            if (strJson.isEmpty()){
                return null;
            }
            // 缓存命中，直接返回
            return JSONObject.parseObject(strJson, TrainingPlan.class);
        }
        // 缓存不存在,查询数据库
        TrainingPlan trainingPlan = trainingPlanMapper.selectTrainingPlanById(id);
        if (trainingPlan == null) {
            // 数据为空，放入空值，解决缓存穿透
            redisTemplate.opsForValue().set(key, "",1, TimeUnit.MINUTES);
            return null;
        }
        // 数据不为空，放入缓存,并添加时间随机值，解决缓存雪崩
        redisTemplate.opsForValue().set(key, JSONObject.toJSONString(trainingPlan), 20 + RandomUtils.nextLong(0, 10), TimeUnit.MINUTES);

        return trainingPlan;
    }
}
